/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DBSelectJFrame.java
 *
 * Created on Aug 12, 2011, 3:19:09 PM
 */
package org.opmviewer.jframes;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.opmviewer.converters.OPM2Neo4j;
import org.opmviewer.core.config.DBRegister;
import org.opmviewer.valueproviders.ValueProvider;

/**
 *
 * @author grzegorz
 */
public class DBSelectJFrame extends JDialog {

    DBRegister dbreg;
    String selectedName;
    String selectedPath;
    boolean selected = false;

    /** Creates new form DBSelectJFrame */
    public DBSelectJFrame(JFrame frame, boolean modal) {
        super(frame, modal);
        dbreg = new DBRegister();
        initComponents();
        dbTable.setRowSelectionAllowed(rootPaneCheckingEnabled);
        dbTable.setRowSelectionAllowed(true);
        pack();
        setLocationRelativeTo(frame);
        setVisible(true);
    }

    public String getSelectedName() {
        return selectedName;
    }

    public String getSelectedPath() {
        return selectedPath;
    }

    public void savePrefs() {
        dbreg.save();
    }

    private void refreshTable() {
        dbTable.setModel(new DefaultTableModel());
        dbTable.setModel(dbreg);
    }

    public boolean selected() {
        return selected;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        dbTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        addButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        visButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        dbTable.setModel(dbreg);
        dbTable.setCellSelectionEnabled(false);
        jScrollPane1.setViewportView(dbTable);
        dbTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jLabel1.setText("Select a Neo4j database below:");

        addButton.setText("Add ...");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        visButton.setText("Visualize");
        visButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                visButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Refresh");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(visButton, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(147, 147, 147)
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
                            .addComponent(deleteButton, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
                            .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))))
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addButton)
                        .addGap(18, 18, 18)
                        .addComponent(deleteButton)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(visButton)
                    .addComponent(jButton1))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        DBAddJFrame dbAddJF = new DBAddJFrame(this);
        if (!dbAddJF.confirmed()) {
            return;
        }
        System.err.println("Continue");
        final String dbLoc = dbAddJF.getDBLocation();
        final String dbName = dbAddJF.getDBName();
        final String opmFile = dbAddJF.getOPMFileName();
        final ValueProvider vp = dbAddJF.getValueProvider();
        final DBSelectJFrame frame = this;
        // SwingWorker worker = new SwingWorker<Void, Void>() {

        //   @Override
        // protected Void doInBackground() throws Exception {
        OPM2Neo4j opm2neo = new OPM2Neo4j(vp);
        opm2neo.createNeo4jGraph(opmFile, dbName, dbLoc);
        try {
            dbreg.addDB(dbName, dbLoc);
            refreshTable();
            System.err.println(dbreg.getValueAt(0, 1));
            System.err.println(dbreg.getColumnCount());
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(frame,
                    "Error while adding database!", "ERROR",
                    JOptionPane.ERROR_MESSAGE);

        } finally {
            //       return null;
        }
        //      }
        //};
        // worker.execute();
    }//GEN-LAST:event_addButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed

        try {
            dbreg.removeDB(dbTable.getSelectedRow());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,
                    "Error while deleting database!", "ERROR",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void visButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_visButtonActionPerformed
        int rowNr = dbTable.getSelectedRow();
        if (rowNr == -1) {
            return;
        }
        selectedName = (String) dbreg.getValueAt(rowNr, 0);
        selectedPath = (String) dbreg.getValueAt(rowNr, 1);
        selected = true;
        dispose();
    }//GEN-LAST:event_visButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    }//GEN-LAST:event_jButton2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JTable dbTable;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton visButton;
    // End of variables declaration//GEN-END:variables
}
